package com.alittle.core.view;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alittle.R;

import dev.xesam.androidkit.utils.ViewUtil;


/**
 * 一个临时 Toolbar,兼容以前的形式,后期需要考虑更灵活的界面布局
 */
public final class PseudoToolbar extends Toolbar {

    private View vToolbarActions;

    private TextView vToolbarAction0;
    private TextView vToolbarAction1;
    private TextView vToolbarAction2;

    public PseudoToolbar(Context context) {
        this(context, null, 0);
    }

    public PseudoToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PseudoToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.un_include_pseudo_toolbar, this);
        vToolbarActions = findViewById(R.id.frame_toolbar_actions);
    }

    private TextView getToolbarActionMenuItem0() {
        if (vToolbarAction0 == null) {
            vToolbarAction0 = ViewUtil.findById(this, R.id.frame_toolbar_action_0);
        }
        return vToolbarAction0;
    }

    private TextView getToolbarActionMenuItem1() {
        if (vToolbarAction1 == null) {
            vToolbarAction1 = ViewUtil.findById(this, R.id.frame_toolbar_action_1);
        }
        return vToolbarAction1;
    }

    private TextView getToolbarActionMenuItem2() {
        if (vToolbarAction2 == null) {
            vToolbarAction2 = ViewUtil.findById(this, R.id.frame_toolbar_action_2);
        }
        return vToolbarAction2;
    }

    private void initToolbarActionMenuItem(TextView tv, V4FrameActionMenu item) {
        getToolbarActionMenuItem1().setVisibility(View.VISIBLE);
        tv.setCompoundDrawablesWithIntrinsicBounds(item.getLeftResId(), 0, item.getRightResId(), 0);
        tv.setText(item.getContent());
        if (TextUtils.isEmpty(item.getContent())) {
            tv.setContentDescription(item.getContentDescription());
        } else {
            tv.setContentDescription(item.getContent());
        }
        tv.setOnClickListener(item.getClickListener());
        tv.setEnabled(item.isEnable());
    }

    public void setupToolbarActionMenu(V4FrameActionMenu[] items) {
        getToolbarActionMenuItem0().setOnClickListener(null);
        getToolbarActionMenuItem1().setOnClickListener(null);
        getToolbarActionMenuItem2().setOnClickListener(null);

        if (items == null || items.length == 0) {
            vToolbarActions.setVisibility(View.GONE);
            return;
        } else {
            vToolbarActions.setVisibility(View.VISIBLE);
        }
        V4FrameActionMenu item0 = items[0];
        initToolbarActionMenuItem(getToolbarActionMenuItem0(), item0);

        if (!TextUtils.isEmpty(item0.getContent()) && (item0.getLeftResId() != V4FrameActionMenu.INVALID_RES_ID || item0.getRightResId() != V4FrameActionMenu.INVALID_RES_ID)) {
            findViewById(R.id.frame_toolbar_action_divider_0).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.frame_toolbar_action_divider_0).setVisibility(View.GONE);
        }

        if (items.length > 1) {
            initToolbarActionMenuItem(getToolbarActionMenuItem1(), items[1]);
        } else {
            getToolbarActionMenuItem1().setVisibility(View.GONE);
        }

        if (items.length > 2) {
            initToolbarActionMenuItem(getToolbarActionMenuItem2(), items[2]);
        } else {
            getToolbarActionMenuItem2().setVisibility(View.GONE);
        }
    }
}
